<?php
namespace Admin\Controller;

class UserController extends PublicController {
	function _initialize() {
		parent::_initialize ();
	}
	public function index() {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $map = '';
            $storeId = 0;
        } else {
            $map['u.store_id'] = $store_id['store_id'];
            $storeId = $store_id['store_id'];
        }

		$count = M ("User")->alias('u')->where("flag='1'")->where($map)->count (); // 查询满足要求的总记录数
		$Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
		$show = $Page->show (); // 分页显示输出
		$result = M ("User")->alias("u")->join("meal_store as s on u.store_id=s.store_id")->field("u.*,s.store_name")->limit ( $Page->firstRow . ',' . $Page->listRows )->where($map)->where("flag='1'")->order("u.id desc")->select ();

		$this->assign ( "result", $result );
		$this->assign ( "page", $show ); // 赋值分页输出
		$this->display ();
	}

    public function edit()
    {
        $result = R("Api/Api/editUser", array(I("get.id")));
        $this->assign ( "result", $result );
        $this->display ();
    }

    public function del()
    {
        $result = R("Api/Api/delUser", array(I("get.id")));
        $this->success("删除用户成功！");
    }

    public function save()
    {
        $result = R("Api/Api/saveUser", array(I("post.")));
        $this->success("修改用户成功！",U('Admin/user/index'));
    }

    //---------------------------------店铺管理员---------------------------------------------------------

    /**
     * 店铺管理员列表
     */
    public function store_admin_list()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $data = '';
        } else {
            $data['a.store_id'] = $store_id['store_id']; // 店铺ID
        }

        $result = M("admin")->alias("a")->join("meal_store as s on a.store_id=s.store_id")->field("a.*,s.store_name")->where($data)->where("a.store_id!=0")->select();
        $this->assign("result",$result);
        $this->display();
    }

    /**
     * 店铺管理员加载数据
     */
    public function store_admin_add()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $storeId = 0;
        } else {
            $storeId = $store_id['store_id']; // 店铺ID
        }
        $id = I('get.id');
        $data['id'] = $id;
        $admin = M('admin')->where($data)->find();
        $store = M('store')->select();
        $this->assign('result', $admin);
        $this->assign('store', $store);
        $this->assign('storeId', $storeId);
        $this->display();
    }

    /**
     * 店铺管理员编辑
     */
    public function store_admin_edit()
    {
        $id = I('request.id');
        $data['store_id'] = I('post.store_id');
        $data['user_name'] = I('post.user_name');
        if (I('post.password')) {
            $data['password'] = md5(I('post.password'));
        }

        if ($id) {
            //修改
            $result = M('admin')->where("id='$id'")->save($data);
            if ($result === false) {
                $this->error("操作失败");
            } else {
                $this->success("操作成功",U('Admin/User/store_admin_list'));
            }
        } else {
            //新增
            $data['add_time'] = time();
            $result = M('admin')->add($data);
            if ($result) {
                $this->success("操作成功",U('Admin/User/store_admin_list'));
            } else {
                $this->error("操作失败");
            }
        }
    }

    /**
     * 删除分类
     * Author:边
     */
    public function delAdmin()
    {
        $categoryId = I('get.id');

        $result_delete = M('admin')->delete($categoryId);
        if ($result_delete) {
            $this->success("操作成功", U('Admin/User/store_admin_list'));
        } else {
            $this->error("删除失败", U('Admin/User/store_admin_list'));
        }
    }
}